<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Medal_receipt extends CI_Controller {

    public function Medal_receipt() {
        parent::__construct();
        $this->load->model('user_model');
        //$this -> load -> model('medal_recept_model');
        $this->load->library('administrator_authenticate');
    }

    public function index() {
        $this->administrator_authenticate->check_login();
        /* $this->load->view('admin_summary_view'); */
        //$cat = $this -> db -> get('Officer');
        //$data['row'] = $cat -> result_array();
        $data['officer_functions'] = $this->_select_function();
        $data['official_orders'] = $this->_official_order();
        $data['officers'] = $this->_select_officer();
        $data['medals'] = $this->_select_medal();
        $data['content'] = 'medal_receipt_view';
        $data['header'] = 'មន្រ្តីទទួលមេដាយ';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $this->load->view('admin_template', $data);
    }

    function _select_function() {
        return $this->db->order_by('`order`')->get('Officer_Function')->result_array();
    }

    function _select_officer() {
        return $this->db->query('SELECT ID, name FROM Officer ORDER BY name LIMIT 300')->result_array();
    }

    function _select_medal() {
        return $this->db->query('SELECT ID, full_name FROM Medal ORDER BY `order` LIMIT 300')->result_array();
    }

    function _official_order() {
        return $this->db->query('SELECT ID, Order_Number FROM Official_Order ORDER BY ID LIMIT 300')->result_array();
    }

    public function medal_list() {
        $this->administrator_authenticate->check_login();
        $this->load->model('Medal_model');
        $medals = $this->Medal_model->getMedal(check_get('term'));
        echo json_encode($medals);
    }
    
    public function receipt() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_reception_model');
        $output = $this->medal_reception_model->modelReceiptDatatable();
        echo json_encode($output);
    }
    
    

    public function officer() {
        $this->administrator_authenticate->check_login();
        $this->load->model('officer_model');
        $output = $this->officer_model->officerDatatable();
        echo json_encode($output);
    }

    function do_new_recept() {
        $return = array();
        if ($_POST['officerid'] != 0) { // UPDATE
            $query = $this->db->get_where('Officer', array('name' => check_post('name'), "ID <> " => check_post('officerid')));

            if (!$query->num_rows) {
                $this->db->where('ID', check_post('officerid'));
                $this->db->update('Officer', array(
                    'function_ID' => check_post('function'),
                    'name' => check_post('name'),
                    'note' => check_post('note')
                ));
                $return['result'] = true;
                $return['message'] = 'កែប្រែព័ត៌មានមន្ត្រីបានជោគជ័យ';
            } else {
                $return['result'] = false;
                $return['message'] = 'ឈ្មោះមន្ត្រីធ្លាប់មានរួចហើយក្នុងប្រព័ន្ធ';
            }
        } else { // INSERT
            $query = $this->db->get_where('Officer', array('name' => check_post('name')));
            if (!$query->num_rows) {
                $this->db->insert('Officer', array(
                    'function_ID' => check_post('function'),
                    'name' => check_post('name'),
                    'note' => check_post('note')
                ));
                $return['result'] = true;
                $return['message'] = 'បន្ថែមមន្ត្រីថ្មីបានជោគជ័យ.';
            } else {
                $return['result'] = false;
                $return['message'] = 'ឈ្មោះមន្ត្រីធ្លាប់មានរួចហើយក្នុងប្រព័ន្ធ';
            }
        }
        echo json_encode($return);
    }

    public function read_officer() {
        $this->administrator_authenticate->check_login();
        $this->load->model('officer_model');
        $output = $this->officer_model->read();
        echo json_encode($output[0]);
    }

    public function delete_officer() {
        $this->administrator_authenticate->check_login();
        $this->db->delete('Officer', array('ID' => check_post("ID")));
        $return = array();
        if ($this->db->affected_rows() > 0) {
            $return['result'] = true;
            $return['message'] = 'លុបមន្ត្រីបានជោគជ័យ.';
        }else{
            $return['result'] = false;
            $return['message'] = 'មិនអាចលុបមន្ត្រីនេះបានទេ សូមព្យាយាមម្តងទៀត';
        }
        echo json_encode($return);
    }

}